#include "usart.h"

/**********************************************************
*** ZDT_X42_V2.0步进闭环控制例程
*** 编写作者：ZHANGDATOU
*** 技术支持：张大头闭环伺服
*** 淘宝店铺：https://zhangdatou.taobao.com
*** CSDN博客：http s://blog.csdn.net/zhangdatou666
*** qq交流群：262438510
**********************************************************/

__IO bool rxFrameFlag = false;
__IO uint8_t u_rxCmd[FIFO_SIZE] = {0};
__IO uint8_t u_rxCount = 0;

#define MOTOR_USE_UART2

#define MOTOR_UART      USART2
#define MOTOR_UART_CLK  RCU_USART2
#define MOTOR_GPIO      GPIOB
#define MOTOR_GPIO_CLK  RCU_GPIOB
#define MOTOR_PIN_TX    GPIO_PIN_10
#define MOTOR_PIN_RX    GPIO_PIN_11
#define MOTOR_UART_IRQ  USART2_IRQn

void usart_init(void)
{
    /* enable GPIO clock */
    rcu_periph_clock_enable(MOTOR_GPIO_CLK);

    /* enable USART clock */
    rcu_periph_clock_enable(MOTOR_UART_CLK);
    
    /* connect port to USARTx_Tx */
    gpio_init(MOTOR_GPIO, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, MOTOR_PIN_TX);

    /* connect port to USARTx_Rx */
    gpio_init(MOTOR_GPIO, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, MOTOR_PIN_RX);

    /* configure USART */
    usart_deinit(MOTOR_UART);
    usart_baudrate_set(MOTOR_UART, 115200U);
    usart_transmit_config(MOTOR_UART, USART_TRANSMIT_ENABLE);
    usart_receive_config(MOTOR_UART, USART_RECEIVE_ENABLE);
    //usart_dma_receive_config(UART3, USART_RECEIVE_DMA_ENABLE);
    usart_enable(MOTOR_UART);
#if 1
    /*Enable rcv and transmit*/
    usart_interrupt_enable(MOTOR_UART,USART_INT_RBNE);
    usart_interrupt_enable(MOTOR_UART,USART_INT_IDLE);
    //usart_interrupt_enable(UART3,USART_INT_TBE);
    
    
    
    /* USART interrupt configuration */
    NVIC_SetPriorityGrouping(1);
    NVIC_SetPriority(MOTOR_UART_IRQ,1);
    NVIC_EnableIRQ(MOTOR_UART_IRQ);
#endif
}


/**
    * @brief   USART1中断函数
    * @param   无
    * @retval  无
    */
//void USART1_IRQHandler(void)
#ifdef MOTOR_USE_UART2
void USART2_IRQHandler(void)
#endif
//void UART3_IRQHandler(void)
{
    __IO uint16_t i = 0;
    uint8_t rx_byte;

    /**********************************************************
    *** 串口接收中断
    **********************************************************/
    if((RESET != usart_interrupt_flag_get(MOTOR_UART, USART_INT_FLAG_RBNE)) &&
        (RESET != usart_flag_get(MOTOR_UART, USART_FLAG_RBNE)))
    {
        rx_byte = (uint8_t)usart_data_receive(MOTOR_UART);

        // 未完成一帧数据接收，数据进入缓冲队列
        fifo_enQueue(rx_byte);
        usart_interrupt_flag_clear(MOTOR_UART, USART_INT_FLAG_RBNE);
    }

    /**********************************************************
    *** 串口空闲中断
    **********************************************************/
    else if( RESET != usart_interrupt_flag_get(MOTOR_UART, USART_INT_FLAG_IDLE))
    {
        usart_interrupt_flag_clear(MOTOR_UART, USART_INT_FLAG_IDLE);
        rx_byte = (uint8_t)usart_data_receive(MOTOR_UART);
        // 提取一帧数据命令
        u_rxCount = fifo_queueLength(); for(i=0; i < u_rxCount; i++) { u_rxCmd[i] = fifo_deQueue(); }

        // 一帧数据接收完成，置位帧标志位
        rxFrameFlag = true;
    }
    #if 0
    else if (usart_flag_get(MOTOR_UART, USART_FLAG_RT) != RESET)
    {
        usart_flag_clear(MOTOR_UART, USART_FLAG_RT);

        
    }
    #endif
    else if (usart_flag_get(MOTOR_UART, USART_FLAG_ORERR) != RESET)
    {
        usart_flag_clear(MOTOR_UART , USART_FLAG_ORERR);
    }

}

/**
    * @brief   USART发送多个字节
    * @param   无
    * @retval  无
    */
void usart_SendCmd(uint8_t *cmd, uint8_t len)
{
    __IO uint8_t i = 0;
    
    for(i=0; i < len; i++) { usart_SendByte(cmd[i]); }
}

/**
    * @brief   USART发送一个字节
    * @param   无
    * @retval  无
    */
void usart_SendByte(uint16_t data)
{
    __IO uint16_t t0 = 0;

    usart_data_transmit(MOTOR_UART, (data & (uint16_t)0x01FF));
    while(RESET == usart_flag_get(MOTOR_UART, USART_FLAG_TBE))
    {
        ++t0; if(t0 > 8000) {   return; }
    }
}


